import http from '@/basic/baseHttp/index'
import { netConfigUse } from '@/stores/modules/netConfig'
import type * as T from '@/types/api/index'
import search from '@/basic/search'
import table from '@/basic/table'
import addEdit from '@/basic/addEdit'
import baseHttp from '@/basic/baseHttp'
const systemApi: T.ISystemApi = {
  async getLayout(params) {
    const { getNetConfig: netConfig } = netConfigUse()
    const res: any = await http.request(netConfig.apis.getLayout, params)
    if (res.code === 200) {
      if (res.data.search && res.data.search !== '{}') {
        res.data.search = JSON.parse(res.data.search)
      } else {
        res.data.search = search
      }
      if (res.data.tableLayout && res.data.tableLayout !== '{}') {
        res.data.tableLayout = JSON.parse(res.data.tableLayout)
      } else {
        res.data.tableLayout = table
      }
      if (res.data.addEdit && res.data.addEdit !== '{}') {
        res.data.addEdit = JSON.parse(res.data.addEdit)
      } else {
        res.data.addEdit = addEdit
      }
      res.data.table = res.data.tableLayout
      res.data.http = baseHttp
    }
    return res
  },
  setLayout(data) {
    if (data.search) {
      data.search = JSON.stringify(data.search)
    }
    if (data.tableLayout) {
      data.tableLayout = JSON.stringify(data.tableLayout)
    }
    if (data.addEdit) {
      data.addEdit = JSON.stringify(data.addEdit)
    }
    if (data.detail) {
      data.detail = JSON.stringify(data.detail)
    }
    const { getNetConfig: netConfig } = netConfigUse()
    return http.request(netConfig.apis.setLayout, data)
  },
  async getLayoutEvent(params) {
    const { getNetConfig: netConfig } = netConfigUse()
    const res: any = await http.request(netConfig.apis.getLayoutEvent, params)
    if (res.code === 200) {
      if (res.data.extend) {
        const data = JSON.parse(res.data.extend)
        if (data?.search) {
          res.data.search = data.search
        } else {
          res.data.search = search
        }
        if (data?.tableLayout) {
          res.data.tableLayout = data.tableLayout
        } else {
          res.data.tableLayout = table
        }
        if (data?.addEdit) {
          res.data.addEdit = data.addEdit
        } else {
          res.data.addEdit = addEdit
        }
        if (data?.other) {
          res.data.other = data.other
        } else {
          res.data.other = {}
        }
      } else {
        res.data.search = search
        res.data.tableLayout = table
        res.data.addEdit = addEdit
        res.data.other = {}
      }
      res.data.table = res.data.tableLayout
      res.data.http = baseHttp
    }
    return res
  },
  async setLayoutEvent(data) {
    if (data.addEdit) {
      const params: any = {
        addEdit: data.addEdit,
        search: data.search,
        tableLayout: data.tableLayout
      }
      if (data?.other) {
        params.other = data.other
      }
      data.extend = JSON.stringify(params)
      delete data.addEdit
      delete data.search
      delete data.http
      delete data.tableLayout
      delete data.other
    }
    const { getNetConfig: netConfig } = netConfigUse()
    return http.request(netConfig.apis.eventEdit, data)
  },
  async getBtns(params) {
    const { getNetConfig: netConfig } = netConfigUse()
    const res: any = await http.request(netConfig.apis.eventBtnGetList, params)
    if (res.code === 200) {
      if (res.data.extend) {
        res.data.extend = JSON.parse(res.data.extend)
      } else {
        res.data.extend = {
          showParams: []
        }
      }
    }
    return res
  },
  editBtns(data) {
    if (data.extend) {
      data.extend = JSON.stringify(data.extend)
    }
    const { getNetConfig: netConfig } = netConfigUse()
    return http.request(netConfig.apis.eventBtnEdit, data)
  }
}

export default systemApi
